package com.zxl.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CalcStock {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String cmd = "";
		String s[];
		
		while(true){
			System.out.println("------------------------");
			System.out.println("  1.股票计算；");
			System.out.println("  2.退出；");
			System.out.println("------------------------");
			System.out.println("请选择：");
			
			String command = inputCommand();
			Long st = System.currentTimeMillis();
			if(command != null && !command.equals("")){
				if(command.length() > 1){
					System.out.println("未识别，请重新输入！");
					continue;
				}
				
				int n = Integer.parseInt(command);
				switch(n){
				case 1:
					System.out.println("请输入以下信息，并用空格分隔：");
					System.out.println("盈亏成本\t拥股数量\t现在股价\t要达到的盈亏成本价格");
					System.out.println("12.067\t300\t7.060\t8.060");
					cmd = inputCommand();
					
					cmd = cmd.replaceAll(" +", " ");
					s = cmd.split(" ");
					
					try{
						calcStock(Double.parseDouble(s[0]),Double.parseDouble(s[1]),Double.parseDouble(s[2]),Double.parseDouble(s[3]));
					}catch(Exception e){
						System.out.println("请确认输入的是数值并用空格分隔！");
					}
					break;
				case 2:
					System.exit(0);
				default:
					System.out.println("未识别，请重新输入！");	
				}
			}else{
				System.out.println("不能为空！");
			}
			
			Long et = System.currentTimeMillis();
			Long l = et-st;
			if(l < 1000){
				System.out.println("耗时" + l + "毫秒");
			}else if(l < 60000){
				System.out.println("耗时" + l/1000 + "秒");
			}else{
				System.out.println("耗时" + l/60000 + "分钟");
			}
		
		}
		
	}
	
	public static String calcStock(double b, double bn, double a, double c){
		
//		double a = 7.060; //现在股价
//		double b = 12.067; //盈亏成本
//		double c = a + 0.01; //要达到的盈亏成本价格
		
		double x = 0;
//		double bn = 300; //拥股数量
		
		if(c <= a){
			System.out.println("要达到的盈亏成本价格必须大于现在股价！");
			return null;
		}
		
		System.out.println("计算中……");
		while(true){
			if((b * bn + a * x) / (bn + x) <= c){
				System.out.println("如果您想让该股盈亏成本价格达到" + c + "，您还需要购买" + x + "股，这将花费" + x * a + "元！");
				break;
			}
			x++;
		}
		return null;
	}
	
	/**
	 * 输入命令
	 * @return
	 */
	public static String inputCommand(){
		String command = "";
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		try {
			command = bf.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return command;
	}

}
